package com.kb.service.impl.crm;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kb.entity.crm.CRMSalesOrder;
import com.kb.entity.crm.Customer;
import com.kb.entity.crm.CustomerAddress;
import com.kb.entity.crm.LinkManOfCustomer;
import com.kb.mapper.sqlServer.kbidm.*;
import com.kb.service.crm.CrmService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@DS("SmartQuote_kb_live")
public class CrmServiceImpl implements CrmService {

    @Resource
    private CRMSalesOrderMapper crmSalesOrderMapper;
    @Resource
    private CRMCustomerMapper customerMapper;
    @Resource
    private CustomerAddressMapper customerAddressMapper;
    @Resource
    private KbContactsMapper contactsMapper;
    @Resource
    private CRMParameterValues parameterValues;

    @Override
    public List<CRMSalesOrder> querySalesOrder(String code) {
        LambdaQueryWrapper<CRMSalesOrder> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CRMSalesOrder::getSalesOrder, code);
        return crmSalesOrderMapper.selectList(queryWrapper);
    }

    @Override
    public int del(String code) {
        LambdaQueryWrapper<CRMSalesOrder> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CRMSalesOrder::getSalesOrder, code);
        return crmSalesOrderMapper.delete(queryWrapper);
    }

    @Override
    public List<Customer> queryCustomerByCode(List<String> codeList) {
        LambdaQueryWrapper<Customer> queryWrapper = Wrappers.lambdaQuery();
        if (CollectionUtils.isNotEmpty(codeList)) {
            queryWrapper.in(Customer::getCustcode, codeList);
        }
        List<Customer> customers = customerMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(customers)) {
            return null;
        }
        return customers;
    }

    @Override
    public List<Customer> queryCustomerByParams(Integer Params) {
        LambdaQueryWrapper<Customer> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(Params)) {
            //Active活动   Valid有效
            queryWrapper.eq(Customer::getStatus, "Valid");
        }
        //最近2天
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        // 获取前2天的日期
        calendar.add(Calendar.DATE, -2);
        queryWrapper.ge(Customer::getLastupdate, calendar.getTime());
        return customerMapper.selectList(queryWrapper);
    }

    @Override
    public List<CustomerAddress> queryCustAddress(Long id) {
        LambdaQueryWrapper<CustomerAddress> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CustomerAddress::getCustomerid, id);
        return customerAddressMapper.selectList(queryWrapper);
    }

    @Override
    public List<LinkManOfCustomer> queryContacts(Long id) {
        LambdaQueryWrapper<LinkManOfCustomer> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(LinkManOfCustomer::getCustomerId, id);
        return contactsMapper.selectList(queryWrapper);
    }

    @Override
    public Long insertOrUpdateContacts(LinkManOfCustomer linkManOfCustomer) {
        if (Objects.isNull(linkManOfCustomer.getRecId())){
            contactsMapper.insert(linkManOfCustomer);
        }else {
            contactsMapper.updateById(linkManOfCustomer);
        }
        return linkManOfCustomer.getRecId();
    }

    @Override
    public Long insertOrUpdateAddress(CustomerAddress customerAddress) {
        if (Objects.isNull(customerAddress.getRecid())){
            customerAddressMapper.insert(customerAddress);
        }else {
            customerAddressMapper.updateById(customerAddress);
        }
        return customerAddress.getRecid();
    }

    @Override
    public Long insertOrUpdateCustomer(Customer customer) {
        if (Objects.isNull(customer.getRecid())){
            customerMapper.insert(customer);
        }else {
            customerMapper.updateById(customer);
        }
        return customer.getRecid();
    }

    @Override
    public String queryCustomerIndustryById(String id) {
        return parameterValues.queryCustomerIndustryById(id);
    }

}
